package org.jackysoft.struts2.business;

import java.io.ByteArrayInputStream;
import java.util.Collection;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.entity.ArmDailyProductReporter;
import org.jackysoft.entity.DailyResetDate;
import org.jackysoft.query.Order;
import org.jackysoft.query.Query;
import org.jackysoft.query.QueryField;
import org.jackysoft.service.ArmDailyReporterService;
import org.jackysoft.service.DailyResetDateService;
import org.jackysoft.util.HttpUtil;
import org.jackysoft.util.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;

@Namespace("/reporter/daily")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ArmDaliyProductReporterAction 
extends ReporterCRUDAction<ArmDailyProductReporter> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2813981131500731408L;
	private ArmDailyProductReporter bean;
	private Collection<ArmDailyProductReporter> beans;
	private Collection<DailyResetDate> resetDays;
	private DailyResetDate resetDay;
	private ArmDailyReporterService service;
	private DailyResetDateService resetDateService;
	private boolean regenate=false;
	private DateTime firstDay;
	private DateTime lastDay;
		
	public String preselect() throws Exception {
		
		bean = service.prepareSelect(DateTime.now());
		return SUCCESS;
	}

	
	public String openoffice() throws Exception {				
		byte[] bs = service.getResearchBytes(bean.getId(),regenate);		
		setContentType(HttpUtil.extToMimeType("xls"));
		setContentLength(bs.length);
		setUpLoadFileName("product.xls");
		inputStream = new ByteArrayInputStream(bs);
		return STREAM;
	}
	
	public String savedoc() throws Exception {
		service.updateResearch(bean.getId());
		return "saveok";
	}
	
	public String collate() throws Exception{
		DateTime date = usedate;
		if(date==null)date = DateTime.now();
		byte[] bs = service.collateDaily(date);
		setContentType(HttpUtil.extToMimeType("xls"));
		setContentLength(bs.length);
		setUpLoadFileName(StringUtils.toUtf8String(date.toString("yyyy-MM-dd")+"各单位生产日报.xls"));
		inputStream = new ByteArrayInputStream(bs);
		return DOWNLOAD;
	}
	
	public String savecollate() throws Exception{
		service.updateCollate(bean.getCreateDate());
		return "saveok";
	}
	
	public String dailyCollation() throws Exception{
		DateTime date = usedate;
		if(date==null)date = DateTime.now();
		beans = (service.getDailyCollation(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth()));		
		return "dailyCollation";
	}
	
	public String openone() throws Exception{
		DateTime cal = DateTime.now().withDate(year, month, date);
		service.get(cal, department);
		return  "openone";
	}
	
	
	
	public String publish() throws Exception{
		service.publish(bean.getCreateDate());
		return "publish";
			
	}
	
	public String submited() throws Exception{
		Query qc = new Query();
		QueryField qf = new QueryField();
		qf.setName("finished");
		qf.setValue("true");
		qc.getFields().add(qf);
		Order odr = new Order("createDate");
		odr.setStyle(Order.DESC);
		qc.getOrders().add(odr);	
	
		this.pager = service.query(qc, page, rows);
		this.pager.setActionClass(this.getClass());
		this.pager.setMethod("submited");
		return "submited";
	}
	
	public String submit() throws Exception{
		service.submit(bean,isFinished());
		return "submit";
	}

	public String resetdays() throws Exception{
		resetDays = resetDateService.getDailyResetDates(year);
		return "resetdays";
	}
	
	public String setresetday() throws Exception{
		resetDateService.setDailyResetDate(year, month, firstDay, lastDay);
		return "dailyresetday";
	}
	
	public String saveResetDay() throws Exception{
		
		resetDateService.saveDailyResetDay(term, firstDay, lastDay);
		return "saveDay";
	}	
	
	public String resetday() throws Exception{
		resetDateService.resetDailyResetDate(year, month);
		return "dailyresetday";
	}
	
	public String removeresetday() throws Exception{
		resetDateService.remove(resetDay);
		return SUCCESS;
	}
	
	
	public String updatestore() throws Exception{
		
		service.updateForwardStore(year, month);		
		return "updatestore";
		
	}
	
	
	
	
	public ArmDailyReporterService getService() {
		return service;
	}

	@Resource
	public void setService(ArmDailyReporterService service) {
		this.service = service;
		this.setDataProvider(service);
	}



	public ArmDailyProductReporter getBean() {
		return bean;
	}



	public void setBean(ArmDailyProductReporter bean) {
		this.bean = bean;
	}


	public boolean isRegenate() {
		return regenate;
	}


	public void setRegenate(boolean regenate) {
		this.regenate = regenate;
	}


	public Collection<ArmDailyProductReporter> getBeans() {
		return beans;
	}


	public void setBeans(Collection<ArmDailyProductReporter> beans) {
		this.beans = beans;
	}


	public DateTime getFirstDay() {
		return firstDay;
	}


	public void setFirstDay(DateTime firstDay) {
		this.firstDay = firstDay;
	}


	public DateTime getLastDay() {
		return lastDay;
	}


	public void setLastDay(DateTime lastDay) {
		this.lastDay = lastDay;
	}


	public Collection<DailyResetDate> getResetDays() {
		return resetDays;
	}


	public void setResetDays(Collection<DailyResetDate> resetDays) {
		this.resetDays = resetDays;
	}

	@Resource
	public void setResetDateService(DailyResetDateService resetDateService) {
		this.resetDateService = resetDateService;
	}


	public DailyResetDate getResetDay() {
		return resetDay;
	}


	public void setResetDay(DailyResetDate resetDay) {
		this.resetDay = resetDay;
	}

	
}
